﻿package mortal.game.view.guild.tabpanel
{
    import Message.DB.Tables.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import com.mui.utils.*;
    import extend.language.*;
    import fl.data.*;
    import flash.events.*;
    import flash.text.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.cache.guild.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.guild.cellrender.*;
    import mortal.mvc.core.*;

    public class GuildBuildingPanel extends GuildBasePanel
    {
        private var _bmpTxtGuildLevel:BitmapNumberText;
        private var _txtGuildName:GTextFiled;
        private var _txtGuildResource:GTextFiled;
        private var _btnLevelUpGuild:GButton;
        private var _guildBuildingNameList:GTileList;
        private var _bmpBigBuildingBg:GImageBitmap;
        private var _bmpBigBuilding:GImageBitmap;
        private var _contianerHp:GSprite;
        private var _bgHp:ScaleBitmap;
        private var _HpProgressBar:BaseProgressBar;
        private var _btnRepair:GButton;
        private var _bgBuildingDesc:ScaleBitmap;
        private var _txtBuildingName:GTextFiled;
        private var _txtNextGuildLvNeed:GTextFiled;
        private var _txtBuildingDesc:GTextFiled;
        private var _btnPre:GLoadedButton;
        private var _btnNext:GLoadedButton;
        private var _txtNextLv:GTextFiled;
        private var _guildBuildingIconList:GTileList;
        private var _curPreviewGuildLv:int;
        private var _rightTopBuilingInfo:TGuildStructureConfig;

        public function GuildBuildingPanel()
        {
            super(ResFileConst.guildOtherPanel);
            return;
        }// end function

        override public function layoutUI() : void
        {
            super.layoutUI();
            UIFactory.bg(18, 69, 227, 450, this, ImagesConst.guildInfoBg);
            UIFactory.gBitmap(ImagesConst.guildLevelBg, 50, 86, this);
            this._bmpTxtGuildLevel = UIFactory.bitmapNumberText(103, 112, "StrengthenNumber.png", 32, 40, -6, this);
            this._bmpTxtGuildLevel.text = "24";
            var _loc_1:* = GlobalStyle.textFormatItemGreen;
            _loc_1.align = "center";
            this._txtGuildName = UIFactory.gTextField("", 58, 182, 150, 24, this, _loc_1);
            var _loc_2:* = GlobalStyle.textFormatJiang;
            _loc_2.color = GlobalStyle.colorChenUint;
            UIFactory.gTextField(Language.getString(60024) + "：", 25, 204, 60, 22, this, _loc_2);
            this._txtGuildResource = UIFactory.gTextField("5554/4578456", 87, 204, 130, 22, this);
            this._btnLevelUpGuild = UIFactory.gButton("" + Language.getString(71274), 97, 225, 62, 24, this);
            UIFactory.bg(25, 254, 212, 257, this, ImagesConst.guildNoticeBg);
            UIFactory.gTextField("" + Language.getString(71275), 101, 257, 60, 24, this, GlobalStyle.textFormatAnjin);
            UIFactory.bg(25, 280, 212, 22, this, ImagesConst.TextBgDisable);
            UIFactory.textField("" + Language.getString(71276), 45, 280, 65, 20, this, GlobalStyle.textFormatAnjin);
            UIFactory.textField("" + Language.getString(71277), 102, 280, 65, 20, this, GlobalStyle.textFormatAnjin);
            UIFactory.textField("" + Language.getString(71278), 172, 280, 65, 20, this, GlobalStyle.textFormatAnjin);
            this._guildBuildingNameList = UIFactory.tileList(25, 302, 212, 22 * 9 + 1, this);
            this._guildBuildingNameList.rowHeight = 22;
            this._guildBuildingNameList.columnWidth = 212;
            this._guildBuildingNameList.rowCount = 9;
            this._guildBuildingNameList.columnCount = 1;
            this._guildBuildingNameList.setStyle("cellRenderer", GuildBuildingNameListCellRenderer);
            this._guildBuildingNameList.drawNow();
            this._guildBuildingNameList.configEventListener(Event.CHANGE, this.onGuildBuildingNameListSelectChange);
            this._bmpBigBuildingBg = UIFactory.gImageBitmap(ImagesConst.GuildBuilding_bg_1, 250, 67, this);
            this._bmpBigBuilding = UIFactory.gImageBitmap(ImagesConst.GuildBuilding_Big_1, 300, 77, this);
            var _loc_3:* = GlobalStyle.textFormatHuang;
            _loc_3.size = 14;
            this._txtBuildingName = UIFactory.textField("" + Language.getString(71279), 590, 70, 100, 26, this, _loc_3);
            UIFactory.bitmap(ImagesConst.guildBuildingNextCondition, 545, 95, this);
            this._txtNextGuildLvNeed = UIFactory.textField("" + Language.getString(71280), 610, 92, 80, 22, this, GlobalStyle.textFormatHong);
            UIFactory.bitmap(ImagesConst.guildBuildingDesc, 545, 115, this);
            this._txtBuildingDesc = UIFactory.textField("dsfafd", 545, 135, 166, 160, this);
            this._txtBuildingDesc.multiline = true;
            this._txtBuildingDesc.wordWrap = true;
            this._contianerHp = UIFactory.getUICompoment(GSprite, 264, 297, this);
            this._contianerHp.visible = false;
            pushUIToDisposeVec(UIFactory.bitmap(ImagesConst.guildBuildingHP, 0, 2, this._contianerHp));
            this._btnRepair = UIFactory.gButton("" + Language.getString(71281), 312, 0, 49, 22, this._contianerHp);
            this._HpProgressBar = UICompomentPool.getUICompoment(BaseProgressBar);
            this._HpProgressBar.setBg(ImagesConst.SystemSetBarBg, true, 242, 16);
            this._HpProgressBar.setProgress(ImagesConst.GuildWarSelfProgress, true, 2, 3, 238, 13);
            this._HpProgressBar.setLabel(BaseProgressBar.ProgressBarTextNone);
            this._HpProgressBar.mouseChildren = false;
            this._HpProgressBar.mouseEnabled = true;
            this._HpProgressBar.x = 60;
            this._HpProgressBar.y = 4;
            this._contianerHp.addChild(this._HpProgressBar);
            this._HpProgressBar.setValue(100, 100);
            pushUIToDisposeVec(this._btnRepair);
            pushUIToDisposeVec(this._HpProgressBar);
            UIFactory.bg(250, 334, 470, 185, this);
            UIFactory.bg(250, 332, 468, 28, this, ImagesConst.RegionTitleBg);
            this._txtNextLv = UIFactory.textField("" + Language.getString(71282), 430, 335, 125, 22, this, GlobalStyle.textFormatBai);
            this._btnPre = UIFactory.gLoadedButton(ImagesConst.petBloodPreBtn_upSkin, 390, 334, 24, 25, this);
            this._btnNext = UIFactory.gLoadedButton(ImagesConst.ShopExpansion_upSkin, 540, 334, 24, 25, this);
            this._guildBuildingIconList = UIFactory.tileList(257, 365, 470, 155, this);
            this._guildBuildingIconList.rowHeight = 155;
            this._guildBuildingIconList.columnWidth = 113;
            this._guildBuildingIconList.rowCount = 1;
            this._guildBuildingIconList.setStyle("cellRenderer", GuildBuildingIconListCellRenderer);
            this._curPreviewGuildLv = Cache.instance.guild.selfGuildInfo.baseInfo.level + 1;
            return;
        }// end function

        private function onGuildBuildingNameListSelectChange(event:Event) : void
        {
            var _loc_2:* = this._guildBuildingNameList.selectedItem as TGuildStructureConfig;
            this.updateRtPart(_loc_2);
            return;
        }// end function

        override public function onMouseClick(event:MouseEvent) : void
        {
            var _loc_2:SelfGuildInfo = null;
            var _loc_3:String = null;
            super.onMouseClick(event);
            switch(event.target)
            {
                case this._btnLevelUpGuild:
                {
                    if (!GuildConst.hasPermissions(GuildConst.UpgradeGuild))
                    {
                        MsgManager.showRollTipsMsg("" + Language.getString(71284));
                        return;
                    }
                    _loc_2 = Cache.instance.guild.selfGuildInfo;
                    _loc_3 = Language.getStringByParam(71285, _loc_2.getLevelConfig().upgradeResource, (_loc_2.baseInfo.level + 1));
                    Alert.show(_loc_3, "" + Language.getString(71286), Alert.OK | Alert.CANCEL, null, this.onUpdateGuildAlertSelect);
                    break;
                }
                case this._btnPre:
                {
                    this.updateBuildingIconList(-1);
                    break;
                }
                case this._btnNext:
                {
                    this.updateBuildingIconList(1);
                    break;
                }
                case this._btnRepair:
                {
                    break;
                }
                default:
                {
                    break;
                    break;
                }
            }
            return;
        }// end function

        private function onUpdateGuildAlertSelect(param1:int) : void
        {
            if (param1 == Alert.OK)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.GUILD_LEVEL_UP, null));
            }
            return;
        }// end function

        override public function update() : void
        {
            var _loc_2:Boolean = false;
            super.update();
            var _loc_1:* = Cache.instance.guild.selfGuildInfo;
            this._bmpTxtGuildLevel.text = _loc_1.baseInfo.level.toString();
            if (_loc_1.baseInfo.level < 10)
            {
                this._bmpTxtGuildLevel.x = 116;
            }
            else
            {
                this._bmpTxtGuildLevel.x = 103;
            }
            this._txtGuildName.text = _loc_1.baseInfo.guildName;
            if (GuildLevelTargetConfig.instance.getInfoByLevel((_loc_1.baseInfo.level + 1)) == null)
            {
                this._btnLevelUpGuild.visible = false;
                this._btnLevelUpGuild.toolTipData = null;
            }
            else
            {
                this._btnLevelUpGuild.visible = true;
                this._btnLevelUpGuild.toolTipData = Language.getStringByParam(71287, _loc_1.getLevelConfig().upgradeResource, (_loc_1.baseInfo.level + 1));
                _loc_2 = _loc_1.baseInfo.resource >= _loc_1.getLevelConfig().upgradeResource;
                if (_loc_2)
                {
                    this._btnLevelUpGuild.filters = [FilterConst.itemChooseFilter];
                    this._txtGuildResource.htmlText = _loc_1.baseInfo.resource + "/" + _loc_1.getLevelConfig().upgradeResource;
                }
                else
                {
                    this._btnLevelUpGuild.filters = null;
                    this._txtGuildResource.htmlText = HTMLUtil.addColor(_loc_1.baseInfo.resource.toString(), GlobalStyle.colorHong) + "/" + _loc_1.getLevelConfig().upgradeResource;
                }
            }
            this.updateBuildingNameList();
            this.updateBuildingIconList();
            return;
        }// end function

        private function updateBuildingNameList() : void
        {
            var _loc_3:TGuildStructureConfig = null;
            var _loc_1:* = GuildBuildingConfig.instance.getOpenBuildingList(Cache.instance.guild.selfGuildInfo.baseInfo.level);
            var _loc_2:Array = [];
            for each (_loc_3 in _loc_1)
            {
                
                _loc_2.push(_loc_3);
            }
            _loc_2.sortOn("structureType", Array.NUMERIC);
            this._guildBuildingNameList.dataProvider = new DataProvider(_loc_2);
            this._guildBuildingNameList.drawNow();
            if (this._guildBuildingNameList.selectedIndex == -1)
            {
                this._guildBuildingNameList.selectedIndex = 0;
            }
            this.onGuildBuildingNameListSelectChange(null);
            return;
        }// end function

        private function updateBuildingIconList(param1:int = 0) : void
        {
            var _loc_4:* = undefined;
            this._curPreviewGuildLv = this._curPreviewGuildLv + param1;
            var _loc_2:Array = [];
            var _loc_3:* = GuildBuildingConfig.instance.getAllBuildingList((this._curPreviewGuildLv - 1));
            this._btnPre.visible = _loc_3 != null;
            _loc_3 = GuildBuildingConfig.instance.getAllBuildingList((this._curPreviewGuildLv + 1));
            this._btnNext.visible = _loc_3 != null;
            _loc_3 = GuildBuildingConfig.instance.getNewBuilding(this._curPreviewGuildLv);
            for each (_loc_4 in _loc_3)
            {
                
                _loc_2.push(_loc_4);
            }
            if (_loc_2.length == 0)
            {
                this._curPreviewGuildLv = this._curPreviewGuildLv - param1;
            }
            else
            {
                _loc_2.sortOn("structureType", Array.NUMERIC);
                this._guildBuildingIconList.dataProvider = new DataProvider(_loc_2);
                this._guildBuildingIconList.drawNow();
            }
            this._txtNextLv.htmlText = this._curPreviewGuildLv + Language.getString(71288);
            return;
        }// end function

        private function updateRtPart(param1:TGuildStructureConfig) : void
        {
            if (param1 == null)
            {
                return;
            }
            this._rightTopBuilingInfo = param1;
            this._txtBuildingName.text = param1.structureLevel + Language.getString(71289) + GuildConst.getBuildingNameByType(param1.structureType);
            if (param1.descStr)
            {
                this._txtBuildingDesc.htmlText = param1.descStr;
            }
            var _loc_2:* = GuildBuildingConfig.instance.getNextBuildingLvNeedGuildLv(Cache.instance.guild.selfGuildInfo.baseInfo.level, param1.structureType);
            if (_loc_2 == -1)
            {
                this._txtNextGuildLvNeed.text = "" + Language.getString(71290);
            }
            else
            {
                this._txtNextGuildLvNeed.text = Language.getStringByParam(71291, _loc_2);
            }
            return;
        }// end function

    }
}
